package com.jic.member.impl;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.member.entity.MemDiscountCardPeriodEntity;
import com.jic.member.mapper.MemDiscountCardPeriodEntityMapper;
import com.jic.member.request.MemDiscountCardPeriodRequest;
import com.jic.member.response.MemDiscountCardPeriodResponse;
import com.jic.member.service.MemDiscountCardPeriodService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 折扣卡生成/绑定/解绑批次
 * @since : 2020-07-02
 * @author MJ
 */
@Service
public class MemDiscountCardPeriodServiceImpl implements MemDiscountCardPeriodService {

    @Autowired
    MemDiscountCardPeriodEntityMapper memDiscountCardPeriodEntityMapper;

    @Override
    public PageResult<MemDiscountCardPeriodResponse> queryDiscountCardPeriodList(MemDiscountCardPeriodRequest request) {
        PageResult pageResult = new PageResult();
        MemDiscountCardPeriodEntity memDiscountCardPeriodEntity = new MemDiscountCardPeriodEntity();
        BeanUtils.copyProperties(request, memDiscountCardPeriodEntity);
        Page page=new Page();
        page.setStart((request.getStart()-1)*request.getLimit());
        page.setLimit(request.getLimit());
        List<MemDiscountCardPeriodEntity> list = memDiscountCardPeriodEntityMapper.findOfPage(memDiscountCardPeriodEntity, page);
        List<MemDiscountCardPeriodResponse> rows = list.stream().map(e -> {
            MemDiscountCardPeriodResponse response = new MemDiscountCardPeriodResponse();
            BeanUtils.copyProperties(e, response);
            return response;
        }).collect(Collectors.toList());
        int count = memDiscountCardPeriodEntityMapper.countOfPage(memDiscountCardPeriodEntity);
        pageResult.setRows(rows);
        pageResult.setTotal(count);
        return pageResult;
    }
}
